package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SetMealServiceImpl implements SetMealService {

    @Autowired
    private SetMealDishMapper setMealDishMapper;

    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setMealPageQueryDTO) {
        log.info("setMealPageQueryDTO: {}", setMealPageQueryDTO);
        PageHelper.startPage(setMealPageQueryDTO.getPage(), setMealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setMealDishMapper.pageQuery(setMealPageQueryDTO);
        return new PageResult(page.getTotal() , page.getResult());
    }

    @Transactional
    public void addSetMeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        // 插入套餐数据
        setMealDishMapper.insert(setmeal);
        Long setmealId = setmeal.getId();
        // 保存套餐的n个菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
            setMealDishMapper.insertBatch(setmealDishes);
        }
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    public SetmealVO getSetMealById(Long id) {
        // 查询套餐数据
        Setmeal setmeal = setMealDishMapper.selectByPrimaryKey(id);
        // 查询套餐中的菜品数据
        List<SetmealDish> setmealDishes = setMealDishMapper.selectBySetmealId(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    public void updateSetMeal(SetmealDTO setmealDTO) {
        //更新套餐数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setMealDishMapper.updateByPrimaryKeySelective(setmeal);
        //更新套餐菜品数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes != null && setmealDishes.size() > 0) {
            // 先删除套餐菜品关联数据
            setMealDishMapper.deleteBySetmealId(setmeal.getId());
            // 再插入套餐菜品关联数据
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId());
            });
            setMealDishMapper.insertBatch(setmealDishes);
        }
    }

    /**
     * 删除套餐
     * @param ids
     */
    public void deleteSetMeal(List<Long> ids) {
        //直接删除指定套餐数据
        setMealDishMapper.deleteBatch(ids);
        // 再删除对应菜品信息
        for (Long id : ids) {
            setMealDishMapper.deleteBySetmealId(id);
        }
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

    /**
     * 修改套餐状态
     * @param status
     * @param id
     */
    public void setStatus(Integer status, Long id) {
        setmealMapper.setStatus(status, id);
    }
}
